package com.hsck.order.kds.setup.fragment.cxzd

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.hjq.toast.Toaster
import com.hsck.order.kds.base.bean.CategoryBean
import com.hsck.order.kds.base.bean.SetupBean
import com.hsck.order.kds.base.isnoam.network.livedata.LiveDataBus
import com.hsck.order.kds.base.utils.setOnSingleClickListener
import com.hsck.order.kds.base.isnoam.view.BaseFragment
import com.hsck.order.kds.databinding.FragmentCxzdDetailsBinding
import com.hsck.order.kds.setup.SetupActivity
import kotlinx.android.synthetic.main.common_head_black.*

/**
 * 厨显站点详情
 */
class CxzdDetailsFragment : BaseFragment<FragmentCxzdDetailsBinding>(),
    SetupActivity.UpdatableFragment {


    var type = "Add"
    var topFragment: Fragment? = null

    override fun inflateBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentCxzdDetailsBinding {
        return FragmentCxzdDetailsBinding.inflate(inflater)
    }

    fun setBack() {
        if (topFragment != null) {
            val activity = requireActivity() as SetupActivity
            activity.switchFragment(topFragment!!)
        }
    }

    override fun loadDataOnce() {
        super.loadDataOnce()
        lin_head_black_back.setOnClickListener {
            setBack()
        }
        if (type == "Add") {
            tv_head_black_title.text = "新增厨显站点"
        } else {
            tv_head_black_title.text = "厨显站点详情"
            binding.btnCxzdDetilsDel.visibility=View.VISIBLE

        }
        //获取厨显界面的传值
        LiveDataBus.with<ArrayList<CategoryBean>>("CxjmFragment").observe(this) {
            var name = ""
            it.forEach {
                name = if (name == "") {
                    it.tagName
                } else {
                    "$name,${it.tagName}"
                }
            }
            binding.tvCxzdDetailsCxjm.text = "$name"
        }

        //获取关联档口的传值
        LiveDataBus.with<ArrayList<CategoryBean>>("GldkFragment").observe(this) {
            var name = ""
            it.forEach {
                name = if (name == "") {
                    it.tagName
                } else {
                    "$name,${it.tagName}"
                }
            }
            binding.tvCxzdDetailsGldk.text = "$name"
        }
        //语音提醒的传值
        LiveDataBus.with<ArrayList<SetupBean>>("YytsFragment").observe(this) {
            var name = ""
            it.forEach {
                name = if (name == "") {
                    it.name
                } else {
                    "$name,${it.name}"
                }
            }
            binding.tvCxzdDetailsYyts.text = "$name"
        }
        binding.apply {

            //选择厨显界面
            tvCxzdDetailsCxjm.setOnSingleClickListener {
                var cxjmFragment = CxjmFragment()//选择厨显界面
                val activity = requireActivity() as SetupActivity
                cxjmFragment.type = "Edit"
                cxjmFragment.topFragment = this@CxzdDetailsFragment
                activity.switchFragment(cxjmFragment)
            }

            //关联档口
            tvCxzdDetailsGldk.setOnSingleClickListener {
                var gldkFragment = GldkFragment()//关联档口
                val activity = requireActivity() as SetupActivity
                gldkFragment.type = "Edit"
                gldkFragment.topFragment = this@CxzdDetailsFragment
                activity.switchFragment(gldkFragment)
            }
            //语音提示
            tvCxzdDetailsYyts.setOnSingleClickListener {
                var yytsFragment = YytsFragment()//关联档口
                val activity = requireActivity() as SetupActivity
                yytsFragment.type = "Edit"
                yytsFragment.topFragment = this@CxzdDetailsFragment
                activity.switchFragment(yytsFragment)
            }
            //打印传菜单
            cbCxzdDetailsPrintDyccd.setOnCheckedChangeListener { compoundButton, isChecked ->
                //关闭时候隐藏下方
                linCxzdDetailsPrint.visibility = if (isChecked) {
                    View.VISIBLE
                } else {
                    View.GONE
                }

            }
            //传菜单打印逻辑-始终打印
            linCxzdDetailsPrintCcdSzdy.setOnClickListener {
                cbCxzdDetailsPrintCcdSzdy.isChecked = true
                cbCxzdDetailsPrintCcdGbh.isChecked = false
            }
            //传菜单打印逻辑-菜品所属桌台信息改变后打印
            linCxzdDetailsPrintCcdGbh.setOnClickListener {
                cbCxzdDetailsPrintCcdSzdy.isChecked = false
                cbCxzdDetailsPrintCcdGbh.isChecked = true
            }
            //打印方式-整单
            linCxzdDetailsPrintDyfsZd.setOnClickListener {
                cbCxzdDetailsPrintDyfsZd.isChecked = true
                cbCxzdDetailsPrintDyfsYcyq.isChecked = false
            }
            //打印方式-一菜一切
            linCxzdDetailsPrintDyfsYcyq.setOnClickListener {
                cbCxzdDetailsPrintDyfsZd.isChecked = false
                cbCxzdDetailsPrintDyfsYcyq.isChecked = true
            }
            //删除站点
            btnCxzdDetilsDel.setOnSingleClickListener {
                setBack()
            }
            //保存
            btnCxzdDetilsSave.setOnSingleClickListener {
                if (checkInput()) {
                    setBack()
                }
            }
        }
    }

    override fun onFragmentVisible() {
        //可见
        if (_binding != null) {

        }
    }

    override fun onFragmentInvisible() {
        //不可见

    }

    /**
     * 检查输入数据是否合法
     */
    private fun checkInput(): Boolean {
        when {
            binding.edtCxzdDetailsName.text.toString().trim() == "" -> {
                Toaster.show("请输入站点名称")
                return false
            }

            binding.tvCxzdDetailsCxjm.text.toString().trim() == "" -> {
                Toaster.show("请选择厨显界面")
                return false
            }
        }
        return true
    }

}